import java.util.*;

//改名字
public class MainF {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        }

        HashMap<Long, List<Long>> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int left = i; //区间左端点
            int right = i + 1; //区间右端点
            while (right < n && arr[left] ==arr[right]) {
                right++;
            }
            long grade = (right - left) * arr[left];
            List<Long> list = map.getOrDefault(arr[left], new ArrayList<>());
            list.add(grade);
            map.put(arr[left], list);
            i = right - 1;
        }

        long ret = 0;
        for (Map.Entry<Long, List<Long>> entry : map.entrySet()) {
            long num = entry.getKey();
            List<Long> list = entry.getValue();
            Collections.sort(list);
            if (list.size() == 1) {
                ret = Math.max(ret, list.get(0));
            } else {
                long num1 = list.get(list.size() - 1); //第一大
                long num2 = list.get(list.size() - 2); //第二大
                ret = Math.max(ret, num1 + num2);
            }
        }

        System.out.println(ret);

        sc.close();
    }

}
